//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.mapper.SysPostMapper;
import com.ruoyi.system.mapper.SysUserPostMapper;
import com.ruoyi.system.service.ISysPostService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysPostServiceImpl implements ISysPostService {
    @Resource
    private SysPostMapper postMapper;
    @Resource
    private SysUserPostMapper userPostMapper;




    public List<SysPost> selectPostList(SysPost post) {
        return this.postMapper.selectPostList(post);
    }

    public List<SysPost> selectPostAll() {
        return this.postMapper.selectPostAll();
    }

    public SysPost selectPostById(Long postId) {
        return this.postMapper.selectPostById(postId);
    }

    public List<Long> selectPostListByUserId(Long userId) {
        return this.postMapper.selectPostListByUserId(userId);
    }

    public String checkPostNameUnique(SysPost post) {
        Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPost info = this.postMapper.checkPostNameUnique(post.getPostName());
        return StringUtils.isNotNull(info) && info.getPostId() != postId ? "1" : "0";
    }

    public String checkPostCodeUnique(SysPost post) {
        Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPost info = this.postMapper.checkPostCodeUnique(post.getPostCode());
        return StringUtils.isNotNull(info) && info.getPostId() != postId ? "1" : "0";
    }

    public int countUserPostById(Long postId) {
        return this.userPostMapper.countUserPostById(postId);
    }

    public int deletePostById(Long postId) {
        return this.postMapper.deletePostById(postId);
    }

    public int deletePostByIds(Long[] postIds) {
        Long[] var2 = postIds;
        int var3 = postIds.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            Long postId = var2[var4];
            SysPost post = this.selectPostById(postId);
            if (this.countUserPostById(postId) > 0) {
                throw new ServiceException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }

        return this.postMapper.deletePostByIds(postIds);
    }

    public int insertPost(SysPost post) {
        return this.postMapper.insertPost(post);
    }

    public int updatePost(SysPost post) {
        return this.postMapper.updatePost(post);
    }
}
